#!/usr/bin/env python3
"""
检查数据库中的记忆数据
"""
import sqlite3

def check_memory_data():
    """检查数据库中的记忆数据"""
    print("🔍 检查数据库中的记忆数据...")
    
    try:
        conn = sqlite3.connect('openmemory.db')
        cursor = conn.cursor()
        
        # 检查表结构
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        print(f"📊 数据库表: {[t[0] for t in tables]}")
        
        # 检查记忆数量
        cursor.execute('SELECT COUNT(*) FROM memories')
        memory_count = cursor.fetchone()[0]
        print(f"📊 当前记忆总数: {memory_count}")
        
        # 检查应用数量
        cursor.execute('SELECT COUNT(*) FROM apps')
        app_count = cursor.fetchone()[0]
        print(f"📊 当前应用总数: {app_count}")
        
        # 检查用户数量
        cursor.execute('SELECT COUNT(*) FROM users')
        user_count = cursor.fetchone()[0]
        print(f"📊 当前用户总数: {user_count}")
        
        # 获取OpenMemory应用详情
        cursor.execute('SELECT id, name, owner_id FROM apps WHERE name = ?', ('OpenMemory',))
        app_info = cursor.fetchone()
        if app_info:
            print(f"✅ OpenMemory应用: ID={app_info[0]}, 所有者={app_info[2]}")
            
            # 获取该应用的记忆
            cursor.execute('SELECT id, content, created_at FROM memories WHERE app_id = ? ORDER BY created_at DESC LIMIT 5', (app_info[0],))
            memories = cursor.fetchall()
            print(f"📝 OpenMemory应用的记忆 ({len(memories)}条):")
            for i, (mid, content, created) in enumerate(memories, 1):
                created_str = str(created)[:19] if created else "未知"
                print(f"  {i}. [{created_str}] {content[:60]}...")
        else:
            print("❌ OpenMemory应用未找到")
            
        # 检查所有应用
        cursor.execute('SELECT id, name, owner_id FROM apps')
        all_apps = cursor.fetchall()
        print(f"🎯 所有应用 ({len(all_apps)}个):")
        for app in all_apps:
            print(f"  - {app[1]} (ID: {app[0]})")
            
        conn.close()
        return True
        
    except Exception as e:
        print(f"❌ 数据库检查失败: {e}")
        return False

if __name__ == "__main__":
    check_memory_data()